<template>
  <div class="notice-list">
    <el-button type="primary" @click="addImage">添加轮播图</el-button>
    <el-card>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        border
      >
        <el-table-column
          prop="title"
          label="标题"
          align="center"
        />
        <el-table-column
          prop="sub_title"
          label="子标题"
          align="center"
        />
        <el-table-column
          prop="picture"
          label="图片"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <input-image :src="scope.row.picture" />
          </template>
        </el-table-column>
        <el-table-column
          prop="h5"
          label="H5页面地址"
          align="center"
        />
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="deleteImg(scope.row, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      title="添加轮播图"
      :visible.sync="dialogVisible"
      width="40%"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="标题：" prop="title">
          <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="子标题：" prop="subTitle">
          <el-input v-model="ruleForm.subTitle" />
        </el-form-item>
        <el-form-item label="图片：" prop="picture">
          <el-upload
            class="avatar-uploader"
            :action="baseUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="ruleForm.picture" :src="ruleForm.picture" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="h5页面地址：" prop="h5">
          <el-input v-model="ruleForm.h5" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { carouselSearch, carouselDelete, carouselSave } from '@/api/user'
import inputImage from '@/components/_inputImage'
export default {
  components: {
    inputImage
  },
  data() {
    return {
      tableData: [],
      loading: false,
      dialogVisible: false,
      ruleForm: {
        picture: ''
      },
      baseUrl: `${process.env.VUE_APP_BASE_API}/platform/upload/img`,
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        subTitle: [
          { required: true, message: '请输入子标题', trigger: 'blur' }
        ],
        picture: [
          { required: true, message: '请上传图片', trigger: 'blur' }
        ],
        h5: [
          { required: true, message: '请输入h5页面地址', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.carouselSearch()
  },
  methods: {
    carouselSearch() {
      this.loading = true
      carouselSearch().then(res => {
        this.tableData = res.list
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    deleteImg(row, index) {
      this.$confirm('确认删除当前轮播图吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        carouselDelete({
          id: row.id
        }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.tableData.splice(index, 1)
        })
      })
    },
    addImage() {
      this.dialogVisible = true
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          carouselSave(this.ruleForm).then(res => {
            this.$message({
              type: 'success',
              message: '添加成功!'
            })
            this.carouselSearch()
            this.dialogVisible = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleAvatarSuccess(res, file) {
      this.ruleForm.picture = file.response.data.url
    }
  }
}
</script>
<style lang="scss">
.notice-list {
  .el-card {

  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>

